package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import mest.entity.modeling_dictionaries;
import mest.entity.model_file;
import mest.service.cxj.ModelFileService;
import mest.service.cxj.ModelingDictionariesService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("modelFile")
@CrossOrigin
public class ModelFileController {

    @Autowired
    private ModelFileService modelFileService;

    @Autowired
    private ModelingDictionariesService modelingDictionariesService;

    @Autowired
    private DBService dbService;

    /**
     * 提供模具档案流水号
     *
     * @return
     */
    @GetMapping("getCode")
    public String getCode() {
        String theirDb=dbService.getCompanyDB();
        modeling_dictionaries modelingdictionaries = modelingDictionariesService.selectModelingDictionaries(theirDb);
        int modelFileNo = modelingdictionaries.getModelfileNo();
        return NumberConst.ModelFile_No + "-" + modelFileNo;
    }

    /**
     * 查询全部
     * @param page
     * @param limit
     * @param modelName
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit,
                            @RequestParam(required = false) String modelName) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<model_file> modelFileList = modelFileService.selectAllModelFile(page, limit, modelName,theirDb);
            Integer count = modelFileService.selectCountModelFile(modelName,theirDb);
            resultObj = new ResultObj(0, "success", count, modelFileList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 添加模具档案
     *
     * @param modelFile
     * @return
     */
    @PostMapping("insertModel")
    public String insertModel(@RequestBody model_file modelFile) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        ResultObj obj = modelFileService.insertModelFile(modelFile, theirDb);
        return JSON.toJSONString(obj);
    }

    /**
     * 修改模具档案
     *
     * @param modelFile
     * @return
     */
    @PutMapping("modifyModel")
    public String modifyModel(@RequestBody model_file modelFile) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (modelFileService.modifyModelFile(modelFile,theirDb)) {
                resultObj = ResultObj.builder().code(0).msg("修改成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("修改失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 删除模具档案（假删）
     *
     * @param id
     * @return
     */
    @DeleteMapping("delModel")
    public String delModel(@RequestParam Integer id) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            if (modelFileService.deleteModelFile(id,theirDb)) {
                resultObj = ResultObj.builder().code(0).msg("删除成功").build();
            } else {
                resultObj = ResultObj.builder().code(-1).msg("删除失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

}
